/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Other;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 *
 * @author Envy
 */
public class EncryptSymmetric {

    public byte[] EncryptSymmetric(String data, String sck_path) throws InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException {

        try {
            //Reading secretkey from file
//            DataInputStream dis = new DataInputStream(new FileInputStream(sck_path));                        

            byte[] buff = sck_path.getBytes();
            
            DESKeySpec ks = new DESKeySpec(buff);
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey myDesKey = skf.generateSecret(ks);

            Cipher desCipher;
            // Create the cipher 
            desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            byte[] text = data.getBytes();
            try {
                desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
                byte[] result = desCipher.doFinal(text);
                return result;
                // System.out.println(textEncrypted);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (NoSuchAlgorithmException ex) {
            System.out.printf(ex.toString());
        }
        return null;
    }
}
